﻿using System;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;

namespace Lwllm.ArcengineProxy.Common
{
    public static class ArcgisLicenseInitializer
    {
        [Obsolete("请使用EsriInit方法")]
        public static esriLicenseStatus Init(esriLicenseProductCode esriLicenseProductCode)
        {
            IAoInitialize aoInitialize = new AoInitializeClass();
            var licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
            licenseStatus = aoInitialize.Initialize(esriLicenseProductCode);
            return licenseStatus;
        }

        /// <summary>
        ///     初始化license
        /// </summary>
        /// <param name="esriLicenseProductCode">esri产品类型</param>
        /// <param name="errorAction">初始化错误后执行的操作</param>
        /// <returns>是否成功初始化</returns>
        public static bool EsriInit(esriLicenseProductCode esriLicenseProductCode, Action errorAction = null)
        {
            if (!RuntimeManager.Bind(ProductCode.Engine))
            {
                if (!RuntimeManager.Bind(ProductCode.Desktop))
                {
                    errorAction?.Invoke();
                    return false;
                }
            }

            IAoInitialize aoInitialize = new AoInitializeClass();
            aoInitialize.Initialize(esriLicenseProductCode);
            return true;
        }
    }
}